const { goodsModel } = require('../../models/goods')
const { cartModel } = require('../../models/carts')
// 工具库
const { end } = require('../../utils/tool')

// 主页接口
module.exports = class Home {
  // 获取商品列表数据
  static async getGoods(ctx) {
    let query = ctx.query;
    //默认第一页
    let page = query.page ? parseInt(query.page) : 1;
    // 默认查询10条
    let size = query.size ? parseInt(query.size) : 10;
    // 分页查询数据库
    let data = await goodsModel.find({}).skip((page - 1) * size).limit(size);
    // 获取总条数
    let count = await goodsModel.count()
    ctx.body = {
      status: 200,
      msg: '获取成功',
      count: count,
      data: data
    }
  }
  // 获取商品详情数据
  static async getGoodsDetails(ctx) {
    let id = ctx.query.id;
    let data = await goodsModel.find({ _id: id });
    let cartsCount = await cartModel.find({}).count();
    console.log(cartsCount)
    ctx.body = {
      status: 200,
      msg: '获取成功',
      data: data[0],
      carts_count:cartsCount
    }
  }
}